#include "changeainfo.h"
#include "ui_changeainfo.h"
#include<QSqlQuery>
#include<QMessageBox>
#include<QSqlRecord>
#include"forma.h"
#include"admininfo.h"
#include"admin.h"
#include<QDebug>
#include"login.h"
//extern QString A2 = Admin::A1;

changeainfo::changeainfo(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::changeainfo)
{
    ui->setupUi(this);
}

changeainfo::~changeainfo()
{
    delete ui;
}

void changeainfo::showchangeainfoW()
{
    emit signalchangeainfo();
    this->show();
}

void changeainfo::showAW()
{
//qDebug()<<"++++++++++++++++"<<A2;

    Admin *aw = new Admin;
//    str = aw->A1;
      qDebug()<<"++++++++++++++++"<<str;
    aw->show();
    aw->selectBoxSlot(str);
}
void changeainfo::on_pb_ok1_clicked()
{
    FormA forma;
    AdminInfo admin_info = forma.ainfo;
    QString id = forma.aid;
    QString sex = ui->lineEdit_Sex->text();
    QString name = ui->lineEdit_Name->text();
    QString birth = ui->lineEdit_Birth->text();
    QSqlQuery query;
    QString queryString =
            QString("update AdmintorInfoTable set Sex="+sex +", Name = "+
                    name+",Birth = "+birth+"where ID = '%1'").arg(id);
    if(query.exec(queryString))
    {
        int id_id = query.record().indexOf("ID");
        int Name_id = query.record().indexOf("Name");
        int Sex_id = query.record().indexOf("Sex");
        int Birth_id = query.record().indexOf("Birth");
        int pswd_id = query.record().indexOf("loginpw");
        while(query.next())
        {
            QString id = query.value(id_id).toString();
            QString Name = query.value(Name_id).toString();
            QString pswd = query.value(pswd_id).toString();
            QString Sex = query.value(Sex_id).toString();
            QString Birth = query.value(Birth_id).toString();
            admin_info.updateAdminInfo(id,Name,Sex, Birth, pswd);
          }
        }else{
            QMessageBox msgBox;
            msgBox.setText("Failed:updata people !!");
            msgBox.setInformativeText("Please try again !");
            msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
            msgBox.setDefaultButton(QMessageBox::Retry);

            int ret = msgBox.exec();
            switch (ret) {
            case QMessageBox::Retry:
              ui->lineEdit_Sex->setFocus();
              break;
            case QMessageBox::Cancel:
              this->close();
              break;
            default:
              // should never be reached
              break;
            }
          }
}

void changeainfo::on_pb_cancel1_clicked()
{
    this->close();
    this->showAW();
}
